<template>
  <div class="box">
    <div class="content">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <a @click="submitForm('ruleForm')">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            Login Immediately
          </a>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import "../css/1.css";
import "../css/style.css";
export default {
  name: "",
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post("login/", this.ruleForm).then((res) => {
            console.log(res);
            if (res.data.meta.status == 200) {
              this.$message.success(res.data.meta.msg); //登录成功
              this.$router.push("/"); //跳转到首页
            //   存储token值
            sessionStorage.setItem('token',res.data.data.token)
            //存储用户名
            this.$store.commit('user',res.data.data.username)
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped></style>
